<?php

namespace app\customer;

use app\consts\Code;
use Illuminate\Pagination\LengthAwarePaginator;

trait BizResponse
{
    //业务正常的返回
    public function ok(array $data = [])
    {
        return json([
            "error" => Code::OK ,
            "message" => transL(sprintf("code.%d",Code::OK)),
            "data" => $data
        ]);
    }

    //业务错误的返回
    public function error(int $error,array $data = [])
    {
        return json([
            "error" => $error ,
            "message" => transL(sprintf("code.%d",$error)),
            "data" => $data
        ]);
    }

    //验证器错误的返回
    public function invlid(array $data = [])
    {
        return json([
            "error" => Code::INVALID_PARAMS ,
            "message" => transL(sprintf("code.%d",Code::INVALID_PARAMS)),
            "data" => $data
        ]);
    }

    //分页数据的返回
    public function paginator(LengthAwarePaginator $paginator)
    {
        return json([
            "error" => Code::OK ,
            "message" => transL(sprintf("code.%d",Code::OK)),
            "data" => [
                "list" => $paginator->items(),
                "page" => [
                    "total" => $paginator->total(),
                    "current" => $paginator->currentPage(),
                    "size" => $paginator->perPage()
                ]
            ]
        ]);
    }
}